import { Behaviour, HitTestScript, TextRenderer, GameObject,getGameObjectById,core } from "../gameengine";
import { user } from "../user";
import {champazee}from "../monsters/champazee"
import{victory,defeated, victoryfinal, defeatedfinal}from "../data"
import{RectCollider}from "../rect/RectCollider"
import { ColliderSystem } from "../rect/ColliderSystem";


export class FightBOSS extends Behaviour {
    startbattle=false;
    infight=true;
    bleed=champazee.att-user.def;
    battleresault=0;
    rst:GameObject;
    
    


    // private speedX = 0;
    // private speedY = 0;


    protected onStart() {
        


        

    }

    protected onUpdate(advancedTime: number) {

        const textRender1 = this.displayObject.children[0].getScript(TextRenderer);
        textRender1.text = user.hp.toString();
        const textRender2 = this.displayObject.children[2].getScript(TextRenderer);
        textRender2.text = "VS";
        const textRender3 = this.displayObject.children[3].getScript(TextRenderer);
        textRender3.text = user.att.toString();
        const textRender4 = this.displayObject.children[5].getScript(TextRenderer);
        textRender4.text = "START!";
        const textRender5 = this.displayObject.children[1].getScript(TextRenderer);
        textRender5.text = champazee.hp.toString();
        const textRender6 = this.displayObject.children[4].getScript(TextRenderer);
        textRender6.text = champazee.att.toString();

        
        const re=champazee.hp;
        

        

        if(!this.startbattle){
            window.addEventListener('click',(ev)=>{
                if(true){
                    
                
                       
                        
                        this.startbattle=true;
                          
                    
                    
                    ev.preventDefault();
                }
        
                })
            }
        if(this.startbattle){

            if(this.infight){
                if(champazee.att>user.def){
                    user.hp-=this.bleed;
                }else{
                    user.hp-=0;
                }
                    champazee.hp-=user.att;
                    if(user.hp>0 && champazee.hp>0){
                        this.infight=true;
                    }else{
                        this.infight=false;
                    }
                    if(!this.infight){
                        if(user.hp>=0){
                            this.battleresault=1;
                        }else{
                            this.battleresault=2;
                        }

                    }
                }
        }
        if(this.battleresault==1){
            this.displayObject.parent.removeChild(this.displayObject);
            this.displayObject = null;
            this.rst = core.createPrefab(victoryfinal);
                const scene = getGameObjectById("UIScene");
                scene.addChild(this.rst);
                champazee.hp=re;
                user.gold +=champazee.gold;
               window.addEventListener('click',(ev)=>{
        if(true){
            if (this.rst) {
                //this.rst.parent.children[2].removeAllChildren;
                getGameObjectById('star').getScript(RectCollider).type=0;

                getGameObjectById('star').parent.removeChild(getGameObjectById('star'));

                
                
                  this.rst.parent.removeChild(this.rst)
                  this.rst = null;
                  
            }
            
            ev.preventDefault();
        }

        })
            }

        
        if(this.battleresault==2){
            this.displayObject.parent.removeChild(this.displayObject)
            this.displayObject = null;
            this.rst = core.createPrefab(defeatedfinal);
                const scene = getGameObjectById("UIScene");
                scene.addChild(this.rst);

        }

    }
}